package org.dding.core.system.service;

import java.util.HashMap;
import java.util.Map;

import org.dding.core.log.ILogger;
import org.dding.core.log.LoggerFactory;
import org.dding.core.system.datasource.DataSource;
import org.dding.core.util.StandaloneXmlParser.DataSourceConfig;
import org.dding.msc.exception.StartException;
import org.dding.msc.exception.WaitingException;
import org.dding.msc.service.Service;
import org.dding.msc.service.ServiceName;
import org.dding.msc.service.StartContext;
import org.dding.msc.service.StopContext;

public abstract class AbstractDataSourceService implements
		Service<Map<DataSourceConfig, DataSource>> {

	public static final ILogger log = LoggerFactory
			.getLogger("org.dding.core.service.datasources");

	public static final ServiceName SERVICE_NAME_BASE = ServiceName
			.of("data-source");

	private Map<DataSourceConfig, DataSource> sqlDataSource = new HashMap<DataSourceConfig, DataSource>();

	public synchronized void start(StartContext startContext) throws StartException {

	}

	public synchronized void stop(StopContext stopContext) {
		sqlDataSource = null;
	}

	@Override
	public Map<DataSourceConfig, DataSource> getValue() throws IllegalStateException, IllegalArgumentException {
		return sqlDataSource;
	}

	@Override
	public void waiting(boolean enable) throws WaitingException {

	}

	protected void addDataSource(DataSourceConfig config, DataSource dataSource) {
		sqlDataSource.put(config, dataSource);
	}
}